<?php
include_once 'dbconnect.php';
$sql = new ownedsql();


function clean_note($var)
{
    $var = strip_tags(trim(rtrim(mysql_real_escape_string($var))));
    return $var;
}

function validEmail($email)
{
    $isValid = true;
    $atIndex = strrpos($email, "@");
    if (is_bool($atIndex) && !$atIndex) {
        $isValid = false;
    } else {
        $domain = substr($email, $atIndex + 1);
        $local = substr($email, 0, $atIndex);
        $localLen = strlen($local);
        $domainLen = strlen($domain);
        if ($localLen < 1 || $localLen > 64) {
            // local part length exceeded
            $isValid = false;
        } else
            if ($domainLen < 1 || $domainLen > 255) {
                // domain part length exceeded
                $isValid = false;
            } else
                if ($local[0] == '.' || $local[$localLen - 1] == '.') {
                    // local part starts or ends with '.'
                    $isValid = false;
                } else
                    if (preg_match('/\\.\\./', $local)) {
                        // local part has two consecutive dots
                        $isValid = false;
                    } else
                        if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
                            // character not valid in domain part
                            $isValid = false;
                        } else
                            if (preg_match('/\\.\\./', $domain)) {
                                // domain part has two consecutive dots
                                $isValid = false;
                            } else
                                if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\",
                                    "", $local))) {
                                    // character not valid in local part unless
                                    // local part is quoted
                                    if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\", "", $local))) {
                                        $isValid = false;
                                    }
                                }
        if ($isValid && function_exists('checkdnsrr')) {
            if (!(checkdnsrr($domain, "MX") || checkdnsrr($domain, "A"))) {
                // domain not found in DNS
                $isValid = false;
            }
        }
    }
    return $isValid;
}

class leadsManager
{
    function viewCustomer($cid)
    {
        global $sql, $_SESSION;
        $sql->connect();
        $sql->query("SELECT * FROM 3i_customers LEFT JOIN 3i_userdetails ON 3i_userdetails.uid=3i_customers.uid WHERE cid='$cid'");
        $result = $sql->query;
        $sql->query("SELECT rid, rCity, cityName, dName, rPlace, rPlaceType, rPlaceNo, rTypeName, rSubType, rPrice FROM 3i_rest LEFT JOIN 3i_customers ON  3i_customers.cid=3i_rest.cid LEFT JOIN 3i_userdetails ON 3i_userdetails.uid=3i_rest.uid LEFT JOIN 3i_rest_types ON 3i_rest_types.rTypeID=3i_rest.rType LEFT JOIN 3i_cities ON 3i_cities.cityid=3i_rest.rCity LEFT JOIN 3i_bp_district ON 3i_bp_district.did=3i_rest.rDist WHERE 3i_rest.cid='$cid' AND rActive='1'");
        $result2 = $sql->query;
        $sql->kill();

        $item = mysql_fetch_array($result);
            $note = nl2br($item['cnote']);
?>
            <table id="userDetails">
                <tr>
                    <td>
                        <div id="customerDetail">
                            <fieldset>
                                <legend>Ügfyél - <a href="leads.php?mode=editcustomer&cid=<?php echo
            $item['cid'] ?>">Szerk</a></legend>
                                <table>
                                    <tr>
                                        <td id="label">Név:</td>
                                        <td><?php echo $item['cSname'] . " " . $item['cFname']; ?></td>
                                    </tr>
                                    <tr>
                                        <td id="label">Tel:</td>
                                        <td><?php echo $item['cphone']; ?></td>
                                    </tr>
                                    <tr>
                                        <td id="label">@:</td>
                                        <td><a href="mailto:<?php echo $item['cemail']; ?>"><?php echo
            $item['cemail']; ?></a></td>
                                    </tr>
                                    <tr>
                                        <td id="label">Megj.:</td>
                                        <td><?php echo $note; ?></td>
                                    </tr>
                                </table>
                            </fieldset>
                        </div>
                        <div id="agentDetail">
                            <fieldset>
                                <legend>Referens</legend>
                                <table>
                                    <tr>
                                        <td id="label">Név:</td>
                                        <td><?php echo $item['sName'] . " " . $item['fName']; ?></td>
                                    </tr>
                                    <tr>
                                        <td id="label">Tel:</td>
                                        <td><?php echo $item['phone']; ?></td>
                                    </tr>
                                    <tr>
                                        <td id="label">@:</td>
                                        <td><a href="mailto:<?php echo $item['email']; ?>"><?php echo
            $item['email']; ?></a></td>
                                    </tr>
                                </table>
                            </fieldset>
                        </div>
                    </td>
                    <td>
                        <div id="restList">
                            <fieldset>
                                <legend>Ingatlanok</legend>
                                <table>
                                <?php
                                while ($rest=mysql_fetch_array($result2)) {
                                    if ($rest['rCity'] == 1) {
                                        $district = $rest['dName'] . "., ";
                                    } else {
                                        $district = "";
                                    }
                                    ?>
                                    <tr>
                                        <td id="restHeader">
                                            <a href="leads.php?mode=viewrest&rid=<?php echo $rest['rid'] ?>"><?php echo "#: <b>" . $rest['rid'] . "</b> - " . $rest['cityName'] . ", " . $district . $rest['rPlace'] . " " . $rest['rPlaceType'] . " " . $rest['rPlaceNo']; ?><br />
                                            <?php echo $rest['rPrice'] . " mFt - " . $rest['rTypeName'] . "/" . $rest['rSubType'] ?></a>
                                        </td>
                                    </tr>
                                    <?php
                                }
                                ?>
                                    <tr>
                                        <td><a href="leads.php?mode=newrest&uid=<?php echo $_SESSION['userID'] ?>&cid=<?php echo
            $cid ?>">Új ingatlan</a></td>
                                    </tr>
                                </table>
                            </fieldset>
                        </div>
                        <div id="searchList">
                            <fieldset>
                                <legend>Keresések</legend>
                                <table>
                                    <tr>
                                        <td><a href="">Új keresés</a></td>
                                    </tr>
                                </table>
                            </fieldset>
                        </div>
                    </td>
                </tr>
            </table>
            <?php

    }

    function customerSearchAction()
    {
        global $_POST, $sql;
        $cid = clean_var($_POST['cid']);
        $cname = clean_var($_POST['name']);
        $cemail = clean_var($_POST['email']);
        $cphone = clean_var($_POST['phone']);
        
        //for averting error notice
        if (!empty($cname)) {
            //remove space from cname
            $cname = str_replace(' ', '', $cname);

            //explode cname
            $cname = explode(",", $cname, 2);
            
            $cnameEmpty = false;
        } else {
            $cnameEmpty = true;
        }

        //query builder

        if (!empty($cid)) {
            $cidAdd = " cid='". $cid . "'";
        } else {
            $cidAdd = "";
        }

        if ($cnameEmpty == false) {
            if (!empty($cidAdd)) {
                $and = " AND";
            } else {
                $and = "";
            }
            $cnameAdd = $and . " cSname='" . $cname[0] . "' AND cFname='" . $cname[1] . "'";
        } else {
            $cnameAdd = "";
        }
        
        if (!empty($cemail)) {
            if (!empty($cidAdd) OR !empty($cnameAdd)) {
                $and = " AND";
            } else {
                $and = "";
            }
            $cemailAdd = $and . " cemail='" . $cemail . "'";
        } else {
            $cemailAdd = "";
        }
        
        if (!empty($cphone)) {
            if (!empty($cidAdd) OR !empty($cnameAdd) OR !empty($cemailAdd)) {
                $and = " AND";
            } else {
                $and = "";
            }
            $cphoneAdd = $and . " cphone='" . $cphone . "'";
        } else {
            $cphoneAdd = "";
        }
        
        $sql->connect();
        $query = "SELECT * FROM 3i_customers WHERE" . $cidAdd . $cnameAdd . $cemailAdd . $cphoneAdd . " ORDER BY cSname ASC";
        
        //for debug
        //echo $query;
        
        $sql->query($query);
        $result = $sql->query;
        
        ?>
        <div id="searchTitle">Keresési találatok</div>
        <?php
        
        if (mysql_num_rows($result)==0) {
            echo "<div id=\"searchError\">Nincs találat!";
        }
        
        while ($item = mysql_fetch_array($result)) {
?>
            <table id="customerListElement">
                <tr>
                    <td><span><a href="leads.php?mode=viewcustomer&cid=<?php echo $item['cid'] ?>"><?php echo $item['cSname'] . " " . $item['cFname'] ?></a></span> | Ingatlanok: X | Keresések: X</td>
                    <td rowspan="2" id="customerAction"><a href="leads.php?mode=editcustomer&cid=<?php echo
            $item['cid'] ?>">Szerkesztés</a> | <a  href="leads.php?mode=deletecustomer&cid=<?php echo
                $item['cid'] ?>">Törlés</a></td>
                </tr>
                <tr>
                    <td>tel.: <?php echo $item['cphone'] ?> | @:<a href="mailto:<?php echo
                $item['cemail']; ?>"><?php echo $item['cemail']; ?></a></td>
                </tr>
            </table>
            <?php
        }

    }


    function restSearchScript()
    {

    }

    function restSearch()
    {

    }
}
?>